﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: ngr
 * Date: 16/02/2015
 * Heure: 14:25
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.Data.SQLite;
using System.Windows.Forms;
using System.IO;


namespace Compta.Core.Classes
{
	/// <summary>
	/// Description of SQLiteFacilites.
	/// </summary>
	
	
	
	public class SQLiteFacilites
	{
		/***********************************************************/
		/*                 STRUCT et ENUM                          */
		/***********************************************************/
		/// <summary>
		/// 
		/// </summary>
		enum messageMode{
			ERROR, 
			WARNING,
			INFO
		}
		
		/***********************************************************/
		/*                 P R O P R I E T E S                     */
		/***********************************************************/
		string filename;
		public string Filename
		{
			get{ return filename;}
			set{ filename = value;}
		}
		
		bool isopen;
		
		SQLiteConnection conn;
		
		string msg;
		messageMode msgMode;
		
		public string LastMsg{
			get;set;
		}
		
		/***********************************************************/
		/*                    M E T H O D E S                      */
		/***********************************************************/
		public SQLiteFacilites()
		{
			init();
		}
		
		public SQLiteFacilites(string filename)
		{
			init();
			setFileName(filename);	
		}
		
		public SQLiteFacilites(string filename, bool opendb)
		{
			init();
			setFileName(filename);	
			if (opendb)
			{
				OpenDB(filename, true);
			}
		}
		
		
		
		void init()
		{
			isopen = false;
			this.filename = "";
			msg = "";
			msgMode = messageMode.INFO;
		}
				
		/// <summary>
		/// 
		/// </summary>
		/// <param name="filename"></param>
		public void setFileName(string filename)
		{
			this.filename = filename ;
			
		}
				
	
		
		public void CreateDBFile(string filename)
		{
			if (!filename.Equals(""))
			{
				
				try{
					SQLiteConnection.CreateFile(filename);
				}
				catch (Exception e)
				{
					msg = "Erreur lors de la création de la base " + filename + "\n" +
						   e.Message.Trim();
					msgMode = messageMode.ERROR;
				}
			}
			else{
				msg = "Pas de nom de fichier renseigné. La base n'a pas été créée.";
				msgMode = messageMode.WARNING;
			}
			
			ShowErrorMessage();
		}
		
		/// <summary>
		/// Ouverture d'un fichier de base de données SQLite
		/// </summary>
		/// <param name="Createfile">Créer le fihcier s'il n'existe pas</param>
		public void OpenDB(string filename, bool Createfile)
		{
			if (IsOpen())
			{
				CloseCurrentDbFile();
			}
			
			if (!File.Exists(filename))
			{
				if (Createfile)
				{
					CreateDBFile(filename);
				}
				else{
					string msg = "Impossible d'ouvrir " + filename + "\n" +
						"Le fichier n'existe pas.";
					msgMode = messageMode.ERROR;
					
				}
			}
			
			//Enfin,  si le fichier existe . . .
			if (File.Exists(filename))
			{
				conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
				conn.Open();
				isopen = true;
			}
			
			ShowErrorMessage();
		}
		
		
		public bool IsOpen()
		{
			
			return isopen;
		}
		
		public SQLiteDataReader SelectQuery(string sql)
		{
			if (!IsOpen())
			{
				OpenDB(filename, false);
			}
			
			SQLiteCommand cmd = new SQLiteCommand(sql, conn);			
			return cmd.ExecuteReader();
			
		}
		
		public int NonSelectQuery(string sql)
		{
			if (!IsOpen())
			{
				OpenDB(filename, false);
			}
			
			SQLiteCommand cmd = new SQLiteCommand(sql, conn);
			return cmd.ExecuteNonQuery();
			
		}
		
		public void CloseCurrentDbFile()
		{
			conn.Close();
			this.filename = "";
			isopen = false;
		}
		
		public SQLiteFacilites Clone()
		{
			SQLiteFacilites sf = new SQLiteFacilites();
			sf.filename = Filename;
			sf.conn = new SQLiteConnection(conn);
			
			
			return sf;
		}
		
		
		/// <summary>
		/// Affichage d'un message
		/// </summary>
		void ShowErrorMessage()
		{
			if (!msg.Equals(""))
			{
				MessageBoxIcon ico;
				switch(msgMode){
					case messageMode.INFO:
						ico = MessageBoxIcon.Information;
						break;
					case messageMode.ERROR:
						ico = MessageBoxIcon.Error;
						break;
					case messageMode.WARNING:
						ico = MessageBoxIcon.Warning;
						break;
					default:
						ico = MessageBoxIcon.Information;
						break;
				}
				MessageBox.Show(msg, "Erreur", MessageBoxButtons.OK,ico);
			}
			LastMsg = msg;
			msg = "";
			msgMode = messageMode.INFO;
			
		}
		
	}
}
